/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/controller/game_update.h"
#include "PlanetWars2dRT-core/specific/model/game_players.h"
#include "PlanetWars2dRT-core/specific/model/game_observer.h"
#include "PlanetWars2dRT-core/specific/model/game_observers.h"


EXTERN_C_BEGIN


void
game_update_players_once(game_t* a_game)
{
  const size_t nb_players = game_get_nb_players(a_game);
  for(unsigned char i=0; i < nb_players; ++i)
    {
      player_update_once(game_get_player_no_check(a_game, i), a_game);
    }
}

void
game_update_once(game_t* a_game)
{
  struct observer_data_with_code data;
  data.data = NULL;
  
  data.code = GAME_OBSERVER_CODE_TURN_BEFORE;
  game_notify_observers(a_game, &data);
  
  universe_update_once(&a_game->universe);
  game_update_players_once(a_game);
  ++a_game->turn;
  
  data.code = GAME_OBSERVER_CODE_TURN_AFTER;
  game_notify_observers(a_game, &data);
}

void
game_update(game_t* game, unsigned short nb_times)
{
  for(unsigned short i=0; i < nb_times; ++i)
    {
      game_update_once(game);
    }
}


EXTERN_C_END
